import dayjs from "dayjs";

const conternedTags = [
  // valDepTF
  "ZUTF_DEP_BOKIR",
  "ZUTF_DEP_SAGPI",
  "ZUTF_DEP_UBRAB",
  "ZUTF_DEP_MUMGO",
  "ZUTF_DEP_ATVAX",
  "ZUTF_DEP_LUVEN",
  // valArrTF
  "ZUTF_ARR_MEXAD",
  "ZUTF_ARR_BUPMI",
  "ZUTF_ARR_LADUP",
  // valArrUU
  "ZUUU_ARR_MEXAD",
  "ZUUU_ARR_AKDIK",
  "ZUUU_ARR_EKOKA",
  // valArrNC
  "ZUNC_ARR_IGNAK",
  "ZUNC_ARR_LADUP",
  // valDepMY
  "ZUMY_DEP_SAGPI",
  "ZUMY_DEP_UBRAB",
  // valArrMY
  "ZUMY_ARR_BUPMI",
];

export default function ({ tags, rawData, begTime, opMode }) {
  let idxList = [];
  for (let i = 0; i < tags.length; i++) {
    if (conternedTags.includes(tags[i])) {
      idxList.push(i);
    }
  }

  let taggedData = [];
  for (let i = 0; i < rawData.length; i += 2) {
    if (i == rawData.length - 1) {
      break;
    }
    let day = dayjs(begTime).add(i * 15, "minute");
    let details = {};
    let valArrTF = 0;
    let valArrTFNorth = 0;
    let valArrTFWest = 0;
    let valDepTF = 0;
    let valDepMY = 0;
    let valArrMY = 0;
    let valArrUU = 0;
    let valArrUU6J = 0;
    let valArrUUEKOKA = 0;
    let valArrNC = 0;
    let valMisc = 0;
    let valAll = 0;
    let valDepTFNorth = 0;
    let valDepTFSouth = 0;
    let valDepTFWest = 0;
    let reasons = [];
    let load = 0;
    idxList.forEach((tagIdx) => {
      let val = rawData[i][tagIdx] + rawData[i + 1][tagIdx];
      details[tags[tagIdx]] = val;
      if (tags[tagIdx].includes("ZUTF_ARR")) {
        valArrTF += val;
        valAll += val;
        if (tags[tagIdx] == "ZUTF_ARR_LADUP") {
          valArrTFWest += val;
        } else {
          valArrTFNorth += val;
        }
      } else if (tags[tagIdx].includes("ZUTF_DEP")) {
        valDepTF += val;
        valAll += val;
        if (
          tags[tagIdx] == "ZUTF_DEP_ATVAX" ||
          tags[tagIdx] == "ZUTF_DEP_LUVEN"
        ) {
          valDepTFSouth += val;
        } else if (tags[tagIdx] == "ZUTF_DEP_MUMGO") {
          valDepTFWest += val;
        } else {
          valDepTFNorth += val;
        }
      } else if (tags[tagIdx].includes("ZUMY_DEP")) {
        valDepMY += val;
        valAll += val;
      } else if (tags[tagIdx] == "ZUUU_ARR_EKOKA") {
        valArrUU += val;
        valArrUUEKOKA += val;
        valAll += val;
      } else if (
        tags[tagIdx] == "ZUUU_ARR_MEXAD" ||
        tags[tagIdx] == "ZUUU_ARR_AKDIK"
      ) {
        if (opMode == "NNWJ") {
          valArrUU += val;
          valArrUU6J += val;
          valAll += val;
        }
      } else if (tags[tagIdx].includes("ZUNC_ARR")) {
        valAll += val;
        valArrNC += val;
      } else if (tags[tagIdx] == "ZUMY_ARR_BUPMI") {
        valArrMY += val;
        valAll += val;
      } else {
        valMisc += val;
        valAll += val;
        console.log("[valMisc]", tags[tagIdx], val);
      }
    });
    let isolate = 0;
    load =
      // 天府北线离场乘以1
      // 若有南充进场，天府北线离场乘以1.2
      valDepTFNorth * (valArrNC > 0 ? 1.2 : 1) +
      // 天府MUMGO离场乘以1.3
      // 若有南充进场，天府MUMGO离场乘以1.5
      valDepTFWest * (valArrNC > 0 ? 1.5 : 1.3) +
      // 府南线离场乘以0.3
      valDepTFSouth * 0.3 +
      // 双流EKOKA进场乘以0.8
      valArrUUEKOKA * 0.8 +
      // 双流AKDIK/MEXAD进场乘以0.3
      valArrUU6J * 0.3 +
      // 天府MEXAD/BUPMI进场乘以1.2
      // 若有南充进场，天府MEXAD/BUPMI进场乘以1.3
      valArrTFNorth * (valArrNC > 0 ? 1.3 : 1.2) +
      // 天府LADUP进场乘以0.5
      valArrTFWest * 0.5 +
      // 南充进场乘以4
      valArrNC * 4 +
      // 绵阳进场乘以 0.3
      valArrMY * 0.3 +
      // 绵阳离场乘以 0.5
      // 若天府进场大于 5 (不含)，绵阳离场乘以 0.8
      // 若有南充进场，绵阳离场乘以 1
      // 若同时满足上述两条，绵阳离场乘以 2
      valDepMY *
        (valArrNC > 0 && valArrTF > 5
          ? 2
          : valArrTF > 5
          ? 0.8
          : valArrNC > 0
          ? 1
          : 0.5);
    if (load >= 24) {
      isolate = 1;
    } else {
      isolate = 0;
    }
    if (
      // 天府起飞出北线及进山小于 4 (含)
      valDepTFNorth + valDepTFWest <= 4 ||
      // 天府起飞出北线及进山小于 7 (含) 且 无南充落地
      (valDepTFNorth + valDepTFWest <= 7 && valArrNC == 0) ||
      // 天府落地小于 3 (含)
      valArrTF <= 3 ||
      // 天府落地小于 5 (含) 且 无南充落地 且 绵阳起飞小于 2 (含)
      (valArrTF <= 5 && valArrNC == 0 && valDepMY <= 2)
    ) {
      isolate = 0;
    }
    taggedData.push({
      day,
      valArrTF,
      valArrTFNorth,
      valArrTFWest,
      valDepTF,
      valDepTFNorth,
      valDepTFWest,
      valDepTFSouth,
      valArrUU,
      valArrUU6J,
      valArrUUEKOKA,
      valArrNC,
      valDepMY,
      valArrMY,
      valMisc,
      valAll,
      isolate,
      opMode,
      details,
      load,
    });
  }
  return taggedData;
}
